<?php
/**
 * Created by PhpStorm.
 * User: 牛赞赞
 * Date: 2020/12/25
 * Time: 10:12
 */

namespace App\Game\Logic;

use App\Game\Conf\GameProgress;
use App\Game\Conf\MainCmd;
use App\Game\Conf\SubCmd;
use App\Game\Core\AStrategy;
use App\Game\Core\Djs;
use App\Game\Core\Packet;
use App\Game\Core\Robot;
use App\Game\Core\Room;

class GameHall extends AStrategy
{
    /**
     * 执行方法
     * 1.检查用户是否有正在进行游戏
     * 2.获取房间数据
     */
    public function exec()
    {
        $account = $this->_params['data']['account'];
        $fd = $this->_params['userinfo']['fd'];
        //var_dump('大厅FD:',$fd);
        $data = $this->checkInGame($account);
        $this->pushToCurrUser($fd, $data);
        if ($data['is_game_over']) {
            $room_no = $this->getRoomNo($account);
            $game_type = intval(Room::getRoomData($room_no, 'game_type'));
            if (!$game_type) {
                pushForceQuitMsg($room_no, $account);
            }
        }
    }

    /**
     * 检查用户是否有进行中的游戏
     * @param $account
     * @return array
     */
    public function checkInGame($account)
    {
        $game_conf = config('game');
        $room_map_key = sprintf($game_conf['user_room'], $account);
        $msg = ['is_game_over' => 1, 'room_no' => null, 'account' => $account, 'game_type' => 0, 'pull_in' => 0];

        $room_no = 0;
        if (redis()->exists($room_map_key)) {
            $room_no = redis()->get($room_map_key);
        }
        $room_data_key = sprintf($game_conf['user_room_data'], $room_no);
        // 检查有没正在进行游戏
        if (redis()->exists($room_data_key)) {
            $progress = getGameProgress($room_no);
            $flag = true;
            $djsData = Djs::getDjsPlayerData($account);
            if ($djsData && $djsData['progress'] != Djs::$progress_game_over) {
                $flag = false;
            }
            $game_type = intval(Room::getRoomData($room_no, 'game_type'));
            $msg['game_type'] = $game_type;
            //是否是在等待阶段或者准备阶段或者结算阶段，如果是的，则直接踢出该玩家
            if (in_array($progress, [GameProgress::GAME_JOIN, GameProgress::GAME_PREPARE,
                    GameProgress::GAME_CACULAR]) && $flag) {
                $robots = Robot::getRoomRobot($room_no);
                $robotNum = count($robots);
                $waiters = getRoomWait($room_no);
                $waitNum = count($waiters);
                if ($waitNum - $robotNum - 1 <= 0) {
                    ddTitle('gh 1');
                    clearRoomDataAll($room_no);
                    return $msg;
                }
                pushForceQuitMsg($room_no, $account);
                ddTitle('gh 2');
                return $msg;
            }
            $is_game_over = Room::getRoomData($room_no, 'is_game_over');
            if (empty($is_game_over)) {
                $is_game_over = 0;
            }
            if ($is_game_over == 0) {
                ddTitle('gh 3');
                $msg['is_game_over'] = 0;
                $msg['room_no'] = (int)$room_no;
                $msg['pull_in'] = 1;
            }

            if ($game_type == 1) {//大奖赛
                $djsInfo = Djs::getDjsPlayerData($account);
                if ($djsInfo['end_time'] <= time() && $is_game_over) {
                    ddTitle('gh 3.5');
                    $msg['is_game_over'] = 1;
                    $msg['pull_in'] = 0;
                    $msg['room_no'] = '';
                    $djsInfo['progress'] = Djs::$progress_game_over;
                    Djs::updatePlayerData($account, $djsInfo);
                }
                if ($is_game_over && $djsInfo['progress'] != Djs::$progress_game_over) {
                    ddTitle('gh 3.6');
                    $msg['is_game_over'] = 0;
                    $msg['pull_in'] = 1;
                    $msg['room_no'] = '';
                    $djsInfo['progress'] = Djs::$progress_game_over;
                    Djs::updatePlayerData($account, $djsInfo);
                    sleep(1);
                }
                if (Djs::$progress_game_over) {
                    ddTitle('gh 3.7');
                    $msg['is_game_over'] = 0;
                    $msg['pull_in'] = 1;
                    $msg['room_no'] = '';
                    $isWin = 0;
//                    $uinfo = Room::getRoomData($room_no, 'uinfo');
//                    if ($uinfo) {
//                        $uinfo = json_decode($uinfo, true);
//                        if (isset($uinfo[$account])) {
//                            ddTitle('gh 3.7.1');
//                            dd('uinfo', $uinfo[$account]);
//                            if ($uinfo[$account]['score'] >= 0) {
//                                ddTitle('gh 3.7.2');
//                                $isWin = 1;
//                            }
//                        }
//                    }
                    $djsInfo['status'] = $isWin;
                    Djs::updatePlayerData($account, $djsInfo);
                }
            }

            ddTitle('gh 4');
            return $msg;
        }
        if ($room_no) {
            broadCast($room_no, ['status' => '退出房间', 'chair_id' => getChairIdbyAccount($room_no, $account)], SubCmd::SUB_GAME_ROOM_QUIT_RESP);
            //从房间绑定集合中剔除该玩家
            removeRoomMapByAccount($account);
            removeRoomWaitByAccount($room_no, $account);
            removeRoomChairByAccount($room_no, $account);
            removeGamePrepareByAccount($room_no, $account);
        }
        $playerInfo = Djs::getDjsPlayerData($account);
        if (empty($playerInfo)) {
            ddTitle('gh 5');
            return ['is_game_over' => 1, 'room_no' => null, 'account' => $account, 'game_type' => 0, 'pull_in' => 0];
        }
        $left_time = $playerInfo['end_time'] - time();
        if ($left_time <= 0) {
            ddTitle('gh 6');
            if ($playerInfo['get_coupons'] == 1) {
                return ['is_game_over' => 1, 'room_no' => null, 'account' => $account, 'game_type' => 1, 'pull_in' => 0];
            }
            $playerInfo['progress'] = Djs::$progress_game_over;
            $playerInfo['status'] = 0;
            Djs::updatePlayerData($account, $playerInfo);
            return ['is_game_over' => 0, 'room_no' => null, 'account' => $account, 'game_type' =>
                1, 'pull_in' => 1];
        }
        if ($left_time > 0 && ($playerInfo['status'] == 1) && $playerInfo['get_coupons'] == 0) {
            ddTitle('gh 7');
            // 对局成功
            return ['is_game_over' => 0, 'room_no' => null, 'account' => $account, 'game_type' => 1, 'pull_in' => 1];
        }
        // 失败但可以复活
        if ($left_time > 0 && $playerInfo['alive'] < 3 && $playerInfo['get_coupons'] == 0) {
            ddTitle('gh 8');
            return ['is_game_over' => 0, 'room_no' => null, 'account' => $account, 'game_type' => 1, 'pull_in' => 1];
        } else {
            ddTitle('gh 9');
            return ['is_game_over' => 1, 'room_no' => null, 'account' => $account, 'game_type' => 1, 'pull_in' => 0];
        }

    }

    /**
     * @param $fd
     * @param $msg
     */
    protected function pushToCurrUser($fd, $msg)
    {
        $in_game = Packet::packFormat('OK', 1, $msg);
        $data = Packet::packEncode($in_game, MainCmd::CMD_SYS, SubCmd::SUB_USER_IN_GAME);
        $this->_params['serv']->push($fd, $data, WEBSOCKET_OPCODE_BINARY);
    }
}